package yuque.a1至20.CalculateEContinuously;

/**
 * ClassName: 输出斐波那契数列前n项
 * Package: yuque
 * Description:
 *
 * @Author fjorid
 * @Create 2025/1/5 20:31
 * @Version 1.0
 */
import java.util.Scanner;

class 输出前n项斐波那契数列 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要输出的斐波那契数列的项数: ");
        int n = scanner.nextInt();

        if (n <= 0) {
            System.out.println("请输入一个正整数");
        } else {
            printFibonacciSeries(n);
        }

        scanner.close();
    }

    public static void printFibonacciSeries(int n) {
        long first = 1, second = 1;
        System.out.print("斐波那契数列的前" + n + "项是: ");

        for (int i = 1; i <= n; i++) {
            if (i == 1) {
                System.out.print(first + " ");
                continue;
            }
            if (i == 2) {
                System.out.print(second + " ");
                continue;
            }
            long next = first + second;
            System.out.print(next + " ");
            first = second;
            second = next;
        }
    }
}